﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Portal.Helpers;
using Portal.Settings;
using Styx.Common;

namespace Portal.GUI
{
    public partial class ConfigurationForm : Form
    {
        private static PortalSettings Settings { get { return PortalSettings.Instance; } }

        readonly List<string> _introImage = new List<string>(); 

        
        #region Form Init/Load
        public ConfigurationForm()
        {
            InitializeComponent();
        }

        private void ConfigurationForm_Load(object sender, EventArgs e)
        {
            pictureBox1.ImageLocation = "http://i.imgur.com/0CD71a9.jpg";
            LoadFormSettings();
        }

        private void LoadFormSettings()
        {
            var customCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";
            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;


            #region Items
            UseHealthstones.Checked = Settings.UseHealthstones;
            HealthstonePercent.Value = Settings.HealthstoneHPPercent;
            UseTrinket1.Checked = Settings.UseTrinket1;
            UseTrinket2.Checked = Settings.UseTrinket2;
            cmboTrinket1.SelectedIndex = Settings.Trinket1Condition;
            cmboTrinket2.SelectedIndex = Settings.Trinket2Condition;
            Trinket1LowHPPercent.Value = Settings.Trinket1LowHPValue;
            Trinket1LowManaPercent.Value = Settings.Trinket1LowManaValue;
            Trinket2LowHPPercent.Value = Settings.Trinket2LowHPValue;
            Trinket2LowManaPercent.Value = Settings.Trinket2LowManaValue;
            #endregion

            #region Professions
            UseSynapseSprings.Checked = Settings.UseSynapseSprings;
            cmboSynapseSprings.SelectedIndex = Settings.SynapseSpringsCond;
            UseLifeblood.Checked = Settings.UseLifeblood;
            cmboLifeblood.SelectedIndex = Settings.LifebloodCond;
            LifebloodLowHPValue.Value = Settings.LifebloodLowHPValue;
            #endregion

            #region Racials
            cmboRacialUsage.SelectedIndex = Settings.RacialUsage;
            #endregion

            #region Talents

            // Tier 1
            Talent_UsePoM.Checked = Settings.UsePresenceOfMind;
            Talent_PoMLowHPValue.Value = Settings.PresenceOfMindTargetLowHPValue;
            Talent_cmboPoMCondition.SelectedIndex = Settings.PresenceOfMindCondition;

            Talent_UseBlazingSpeed.Checked = Settings.UseBlazingSpeed;

            Talent_UseIceFloes.Checked = Settings.UseIceFloes;

            // Tier 2
            Talent_UseTemporalShieldWhenCCd.Checked = Settings.UseTemporalShieldWhenCcd;
            Talent_UseTemporalShieldOnLowHP.Checked = Settings.UseTemporalShieldLowHp;
            Talent_TemporalShieldLowHPValue.Value = Settings.UseTemporalShieldLowHpValue;

            Talent_IceBarrierCondition.SelectedIndex = Settings.IceBarrierCondition;
            Talent_IceBarrierLowHPValue.Value = Settings.IceBarrierLowHpValue;

            // Tier 3
            Talent_cmboRingOfFrostCondition.SelectedIndex = Settings.RingOfFrostCondition;
            Talent_RingOfFrostUnitsNearTargetValue.Value = Settings.RingOfFrostUnitsNearTarget;

            Talent_UseIceWard.Checked = Settings.UseIceWard;
            Talent_IceWardOnMe.Checked = Settings.IceWardOnMe;

            Talent_cmboFrostjawCondition.SelectedIndex = Settings.FrostjawCondition;

            // Tier 4
            Talent_UseColdSnapOnLowHP.Checked = Settings.UseColdSnapLowHp;
            Talent_ColdSnapHPValue.Value = Settings.ColdSnapLowHpValue;
            Talent_ColdSnapIceBlockOnCDAndLowHP.Checked = Settings.UseColdSnapIceBlockOnCdAndUnder70Hp;

            // Tier 6 
            Talent_UseInvocation.Checked = Settings.UseInvocation;

            Talent_UseRuneOfPower.Checked = Settings.UseRuneOfPower;

            Talent_IncantersWardLowHP.Checked = Settings.UseIncantersWardOnLowHP;
            Talent_IncantersWardLowHPValue.Value = Convert.ToInt32(Settings.UseIncantersWardOnLowHPValue, customCulture);
            Talent_IncantersWardUnitsTargetingMe.Checked = Settings.UseIncantersWardOnUnitsTargetingMe;
            Talent_IncatersWardUnitsTargetingMeValue.Value = Convert.ToInt32(Settings.UseIncantersWardOnUnitsTargetingMeValue, customCulture);



            #endregion

            #region General - Non Specialization Abilities

            cmboMageArmorType.SelectedIndex = Settings.MageArmor;
            UseConeOfCold.Checked = Settings.UseConeOfCold;
            ConeOfColdOnlyWhenNotSlowed.Checked = Settings.ConeOfColdOnlyWhenNotSlowed;
            UseDeepFreeze.Checked = Settings.UseDeepFreeze;
            UseSpellsteal.Checked = Settings.UseSpellsteal;

            UseAlterTimeOffensive.Checked = Settings.UseAlterTimeOffensive;

            UseFrostNova.Checked = Settings.UseFrostNova;
            FrostNovaOnlyWhenTargetingMe.Checked = Settings.OnlyFrostNovaWhenTargetingMe;
            KillTotems.Checked = Settings.KillTotems;

            UseIceBlock.Checked = Settings.UseIceBlock;
            IceBlockHPValue.Value = Settings.IceBlockLowHpValue;

            UseEvocationLowMana.Checked = Settings.UseEvocationLowMana;
            EvocationLowManaValue.Value = Settings.EvocationLowManaPercent;
            UseEvocationLowHP.Checked = Settings.UseEvocationLowHP;
            EvocationLowHPValue.Value = Settings.EvocationLowHPPercent;

            cmboMirrorImageCondition.SelectedIndex = Settings.MirrorImageCondition;
            MirrorImageTargetLowHPValue.Value = Settings.MirrorImageTargetLowHpValue;

            UseBlinkOnLowHP.Checked = Settings.UseBlinkLowHp;
            UseBlinkOnLowHPValue.Value = Settings.BlinkLowHpValue;
            UseBlinkStunnedRooted.Checked = Settings.UseBlinkStunnedRooted;

            DecurseSelf.Checked = Settings.DecurseSelf;
            DecurseFocus.Checked = Settings.DecurseFocus;

            ConjureManaGem.Checked = Settings.ConjureManaGem;
            UseManaGem.Checked = Settings.UseManaGem;
            ManaGemValue.Value = Settings.ManaGemLowManaValue;

            UseArcaneBrilliance.Checked = Settings.UseArcaneBrilliance;

            UseConjureRefreshments.Checked = Settings.ConjureRefreshments;

            #endregion

            #region Frost

            Frost_EnableAoE.Checked = Settings.Frost_EnableAoE;
            Frost_AoEUnitCount.Value = Convert.ToInt32(Settings.Frost_AoEUnitCount, customCulture);

            UsePetFreeze.Checked = Settings.Frost_UsePetFreeze;

            Frost_FrozenOrbStunnedRooted.Checked = Settings.Frost_FrozenOrbTargetStunnedRooted;
            Frost_radFrozenOrbCooldown.Checked = Settings.Frost_FrozenOrbOnCooldown;
            Frost_FrozenOrbTargetLowHP.Checked = Settings.Frost_FrozenOrbOnTargetLowHP;
            Frost_FrozenOrbTargetLowHPValue.Value = Convert.ToInt32(Settings.Frost_FrozenOrbOnTargetLowHPValue, customCulture);

            Frost_IcyVeinsCondition.SelectedIndex = Settings.IcyVeinsCondition;
            #endregion

            #region Fire

            Fire_EnableAoE.Checked = Settings.Fire_EnableAoE;
            Fire_AoEUnitCount.Value = Settings.Fire_AoEUnitCount;

            Fire_CombustionCooldown.Checked = Settings.Fire_CombustionCooldown;
            Fire_CombustionPoolWithCooldowns.Checked = Settings.Fire_CombustionPoMAlterTimeCombo;

            #endregion

            #region Arcane

            Arcane_EnableAoE.Checked = Settings.Arcane_EnableAoE;
            Arcane_AoEUnitCount.Value = Settings.Arcane_AoEUnitCount;

            Arcane_ArcanePowerCondition.SelectedIndex = Settings.Arcane_ArcanePowerCondition;
            Arcane_TargetLowHPValue.Value = Settings.Arcane_ArcanePowerLowHPValue;

            #endregion

        }

        private void StoreSettingsToInstance()
        {
            var customCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";
            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

            #region Items
            Settings.UseHealthstones = UseHealthstones.Checked;
            Settings.HealthstoneHPPercent = Convert.ToInt32(HealthstonePercent.Value, customCulture);
            Settings.UseTrinket1 = UseTrinket1.Checked;
            Settings.UseTrinket2 = UseTrinket2.Checked;
            Settings.Trinket1Condition = cmboTrinket1.SelectedIndex;
            Settings.Trinket2Condition = cmboTrinket2.SelectedIndex;
            Settings.Trinket1LowHPValue = Convert.ToInt32(Trinket1LowHPPercent.Value, customCulture);
            Settings.Trinket1LowManaValue = Convert.ToInt32(Trinket1LowManaPercent.Value, customCulture);
            Settings.Trinket2LowHPValue = Convert.ToInt32(Trinket2LowHPPercent.Value, customCulture);
            Settings.Trinket2LowManaValue = Convert.ToInt32(Trinket2LowManaPercent.Value, customCulture);
            #endregion
            
            #region Professions
            Settings.SynapseSpringsCond = cmboSynapseSprings.SelectedIndex;
            Settings.UseSynapseSprings = UseSynapseSprings.Checked;
            Settings.UseLifeblood = UseLifeblood.Checked;
            Settings.LifebloodCond = cmboLifeblood.SelectedIndex;
            Settings.LifebloodLowHPValue = Convert.ToInt32(LifebloodLowHPValue.Value, customCulture);
            #endregion

            #region Racials
            Settings.RacialUsage = cmboRacialUsage.SelectedIndex;
            #endregion

            #region Talents

            // Tier 1
            Settings.UsePresenceOfMind = Talent_UsePoM.Checked;
            Settings.PresenceOfMindTargetLowHPValue = Convert.ToInt32(Talent_PoMLowHPValue.Value, customCulture);
            Settings.PresenceOfMindCondition = Talent_cmboPoMCondition.SelectedIndex;

            Settings.UseBlazingSpeed = Talent_UseBlazingSpeed.Checked;

            Settings.UseIceFloes = Talent_UseIceFloes.Checked;

            // Tier 2
            Settings.UseTemporalShieldWhenCcd = Talent_UseTemporalShieldWhenCCd.Checked;
            Settings.UseTemporalShieldLowHp = Talent_UseTemporalShieldOnLowHP.Checked;
            Settings.UseTemporalShieldLowHpValue = Convert.ToInt32(Talent_TemporalShieldLowHPValue.Value, customCulture);

            Settings.IceBarrierCondition = Talent_IceBarrierCondition.SelectedIndex;
            Settings.IceBarrierLowHpValue = Convert.ToInt32(Talent_IceBarrierLowHPValue.Value, customCulture);

            // Tier 3
            Settings.RingOfFrostCondition = Talent_cmboRingOfFrostCondition.SelectedIndex;
            Settings.RingOfFrostUnitsNearTarget = Convert.ToInt32(Talent_RingOfFrostUnitsNearTargetValue.Value, customCulture);

            Settings.UseIceWard = Talent_UseIceWard.Checked;
            Settings.IceWardOnMe = Talent_IceWardOnMe.Checked;

            Settings.FrostjawCondition = Talent_cmboFrostjawCondition.SelectedIndex;

            // Tier 4
            Settings.UseColdSnapLowHp = Talent_UseColdSnapOnLowHP.Checked;
            Settings.ColdSnapLowHpValue = Convert.ToInt32(Talent_ColdSnapHPValue.Value, customCulture);
            Settings.UseColdSnapIceBlockOnCdAndUnder70Hp = Talent_ColdSnapIceBlockOnCDAndLowHP.Checked;

            // Tier 6 
            Settings.UseInvocation = Talent_UseInvocation.Checked;

            Settings.UseRuneOfPower = Talent_UseRuneOfPower.Checked;

            Settings.UseIncantersWardOnLowHP = Talent_IncantersWardLowHP.Checked;
            Settings.UseIncantersWardOnLowHPValue = Convert.ToInt32(Talent_IncantersWardLowHPValue.Value, customCulture);
            Settings.UseIncantersWardOnUnitsTargetingMe = Talent_IncantersWardUnitsTargetingMe.Checked;
            Settings.UseIncantersWardOnUnitsTargetingMeValue = Convert.ToInt32( Talent_IncatersWardUnitsTargetingMeValue.Value, customCulture);



            #endregion

            #region General - Non Specialization Abilities

            Settings.MageArmor = cmboMageArmorType.SelectedIndex;
            Settings.UseConeOfCold = UseConeOfCold.Checked;
            Settings.ConeOfColdOnlyWhenNotSlowed = ConeOfColdOnlyWhenNotSlowed.Checked;
            Settings.UseDeepFreeze = UseDeepFreeze.Checked;
            Settings.UseSpellsteal = UseSpellsteal.Checked;

            Settings.UseAlterTimeOffensive = UseAlterTimeOffensive.Checked;

            Settings.UseFrostNova = UseFrostNova.Checked;
            Settings.OnlyFrostNovaWhenTargetingMe = FrostNovaOnlyWhenTargetingMe.Checked;
            Settings.KillTotems = KillTotems.Checked;

            Settings.UseIceBlock = UseIceBlock.Checked;
            Settings.IceBlockLowHpValue = Convert.ToInt32(IceBlockHPValue.Value, customCulture);

            Settings.UseEvocationLowMana = UseEvocationLowMana.Checked;
            Settings.EvocationLowManaPercent = Convert.ToInt32(EvocationLowManaValue.Value, customCulture);
            Settings.UseEvocationLowHP = UseEvocationLowHP.Checked;
            Settings.EvocationLowHPPercent = Convert.ToInt32(EvocationLowHPValue.Value, customCulture);

            Settings.MirrorImageCondition = cmboMirrorImageCondition.SelectedIndex;
            Settings.MirrorImageTargetLowHpValue = Convert.ToInt32(MirrorImageTargetLowHPValue.Value, customCulture);

            Settings.UseBlinkLowHp = UseBlinkOnLowHP.Checked;
            Settings.BlinkLowHpValue = Convert.ToInt32(UseBlinkOnLowHPValue.Value, customCulture);
            Settings.UseBlinkStunnedRooted = UseBlinkStunnedRooted.Checked;

            Settings.DecurseSelf = DecurseSelf.Checked;
            Settings.DecurseFocus = DecurseFocus.Checked;

            Settings.ConjureManaGem = ConjureManaGem.Checked;
            Settings.UseManaGem = UseManaGem.Checked;
            Settings.ManaGemLowManaValue = Convert.ToInt32(ManaGemValue.Value, customCulture);

            Settings.UseArcaneBrilliance = UseArcaneBrilliance.Checked;

            Settings.ConjureRefreshments = UseConjureRefreshments.Checked;

            #endregion

            #region Frost

            Settings.Frost_EnableAoE = Frost_EnableAoE.Checked;
            Settings.Frost_AoEUnitCount = Convert.ToInt32(Frost_AoEUnitCount.Value, customCulture);

            Settings.Frost_UsePetFreeze = UsePetFreeze.Checked;

            Settings.Frost_FrozenOrbTargetStunnedRooted = Frost_FrozenOrbStunnedRooted.Checked;
            Settings.Frost_FrozenOrbOnCooldown = Frost_radFrozenOrbCooldown.Checked;
            Settings.Frost_FrozenOrbOnTargetLowHP = Frost_FrozenOrbTargetLowHP.Checked;
            Settings.Frost_FrozenOrbOnTargetLowHPValue = Convert.ToInt32(Frost_FrozenOrbTargetLowHPValue.Value, customCulture);

            Settings.IcyVeinsCondition = Frost_IcyVeinsCondition.SelectedIndex;

            #endregion

            #region Fire

            Settings.Fire_EnableAoE = Fire_EnableAoE.Checked;
            Settings.Fire_AoEUnitCount = Convert.ToInt32(Fire_AoEUnitCount.Value, customCulture);;

            Settings.Fire_CombustionCooldown = Fire_CombustionCooldown.Checked;
            Settings.Fire_CombustionPoMAlterTimeCombo = Fire_CombustionPoolWithCooldowns.Checked;
            #endregion

            #region Arcane

            Settings.Arcane_EnableAoE = Arcane_EnableAoE.Checked;
            Settings.Arcane_AoEUnitCount = Convert.ToInt32(Arcane_AoEUnitCount.Value, customCulture);

            Settings.Arcane_ArcanePowerCondition = Arcane_ArcanePowerCondition.SelectedIndex;
            Settings.Arcane_ArcanePowerLowHPValue = Convert.ToInt32(Arcane_TargetLowHPValue.Value, customCulture);

            #endregion

        }
        #endregion

        #region Button Hover Stuff
        private void btnSave_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver(btnSave);
        }

        private void btnSave_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave(btnSave);
        }

        private void btnCancel_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver(btnCancel);
        }

        private void btnCancel_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave(btnCancel);
        }

        private void btnLoadFromFile_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver(btnLoadFromFile);
        }

        private void btnLoadFromFile_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave(btnLoadFromFile);
        }

        private void btnSaveToFile_MouseEnter(object sender, EventArgs e)
        {
            ButtonHoverOver(btnSaveToFile);
        }

        private void btnSaveToFile_MouseLeave(object sender, EventArgs e)
        {
            ButtonLeave(btnSaveToFile);
        }

        private void ButtonHoverOver(Control button)
        {
            button.BackColor = Color.LightSkyBlue;
            button.ForeColor = Color.White;
        }

        private void ButtonLeave(Control button)
        {
            button.BackColor = Color.Gray;
            button.ForeColor = Color.White;
        }

        private void ButtonClick(Control button)
        {
            button.BackColor = Color.White;
            button.ForeColor = Color.DodgerBlue;
        }



        #endregion

        #region Button Click Actions
        private void btnSave_Click(object sender, EventArgs e)
        {
            ButtonClick(btnSave);
            StoreSettingsToInstance();
            Settings.Save();
            Settings.NonStaticLogSettings();
            Close();
            ButtonLeave(btnSave);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ButtonClick(btnCancel);
            Settings.Load();
            Close();
            ButtonLeave(btnCancel);
        }

        private void btnLoadFromFile_Click(object sender, EventArgs e)
        {
            ButtonClick(btnLoadFromFile);
            PortalSettings.LoadFile();
            LoadFormSettings();
            ButtonLeave(btnLoadFromFile);
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            ButtonClick(btnSaveToFile);
            StoreSettingsToInstance();
            PortalSettings.SaveFile();
            ButtonLeave(btnSaveToFile);
        }


        #endregion

        #region State Change - Enable/Disable Components

        private void cmboQuickLoad_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }


        

        private void Talent_cmboRingOfFrostCondition_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void Talent_cmboFrostjawCondition_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void Talent_cmboPoMCondition_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void Talent_IceBarrierCondition_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            ComboBox senderComboBox = (ComboBox)sender;
            int width = senderComboBox.DropDownWidth;
            Graphics g = senderComboBox.CreateGraphics();
            Font font = senderComboBox.Font;
            int vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;
            foreach (string s in ((ComboBox)sender).Items)
            {
                newWidth = (int)g.MeasureString(s, font).Width
                    + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }
        #endregion State Change - Enable/Disable Components

    }
}
